﻿Imports ADOEntity
Imports System.Data
Imports System.IO

Partial Class Administrator_SystemConfigEdit
    Inherits Web.UI.BasePage

    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim systemConfigEntity As systemConfigEntities
    Dim strMsg As String = ""

#Region " Even Page"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        btnOK.Text = Resources.resButton.btnSave
        btnCancel.Text = Resources.resButton.btnCancel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            chkIsLogin()
            clsSessionMenuRight = Session("ClsMenuRight")

            If Not (IsPostBack) Then
                CreateControlMP()
                systemConfigEntity = New systemConfigEntities
                Dim qry = (From systemConfig In systemConfigEntity.SSYSTEMCONFIG
                           Order By systemConfig.CONFIGINDEX).ToList
                txtExpirePwdAdmin.Text = qry(0).CONFIGNUM
                hfExpirePwdAdmin.Value = qry(0).CONFIGSELECT

                txtExpirePwdUser.Text = qry(1).CONFIGNUM
                hfExpirePwdUser.Value = qry(1).CONFIGSELECT

                txtNumFalseLogin.Text = qry(2).CONFIGNUM
                hfNumFalseLogin.Value = qry(2).CONFIGSELECT

                txtGetLogFile.Text = qry(3).CONFIGNUM
                hfGetLogFile.Value = qry(3).CONFIGSELECT

                txtAlertBeforeExpirePwd.Text = qry(4).CONFIGNUM
                hfAlertBeforeExpirePwd.Value = qry(4).CONFIGSELECT

                txtAlertBeforeExpireUser.Text = qry(5).CONFIGNUM
                hfAlertBeforeExpireUser.Value = qry(5).CONFIGSELECT

                txtDefaultExpirePwdUser.Text = qry(6).CONFIGNUM
                hfDefaultExpirePwdUser.Value = qry(6).CONFIGSELECT

                Dim clsEn As New clsEdmUser
                txtDefaultPassword.Attributes.Add("value", clsEn.DecryPass(qry(7).CONFIGSTR))
                hfDefaultPassword.Value = qry(7).CONFIGSELECT
                clsEn = Nothing

            End If

        Catch ex As Exception

        End Try


    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ตั้งค่าระบบ"
    End Sub

#End Region

#Region " Even Button "
    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim chkPassSave As Int16 = 0
        Try
            If chkBeforeSave() = True Then
                systemConfigEntity = New systemConfigEntities
                Dim qry = (From systemConfig In systemConfigEntity.SSYSTEMCONFIG
                           Order By systemConfig.CONFIGINDEX).ToList
                'txtPoscode.Text = qry(0).POSCODE
                qry(0).CONFIGNUM = txtExpirePwdAdmin.Text
                qry(1).CONFIGNUM = txtExpirePwdUser.Text
                qry(2).CONFIGNUM = txtNumFalseLogin.Text
                qry(3).CONFIGNUM = txtGetLogFile.Text
                qry(4).CONFIGNUM = txtAlertBeforeExpirePwd.Text
                qry(5).CONFIGNUM = txtAlertBeforeExpireUser.Text
                qry(6).CONFIGNUM = txtDefaultExpirePwdUser.Text
                'qry(7).CONFIGNUM = txtDefaultPassword.Text

                Dim clsEn As New clsEdmUser
                Dim dataPass As String = ""
                dataPass = clsEn.converPassLoginToDB(txtDefaultPassword.Text)
                createPurgeSQL("D:\PurgeDATA\", "PurgeSQL.SQL", txtGetLogFile.Text)

                If (clsEn.ErrExp = "") Then
                    qry(7).CONFIGSTR = dataPass
                Else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
                    Exit Sub
                End If
                clsEn = Nothing

                systemConfigEntity.SaveChanges()
                chkPassSave = 1
            Else
                Dim tempPwd As String = ""
                tempPwd = txtDefaultPassword.Text
                txtDefaultPassword.Attributes.Clear()
                txtDefaultPassword.Attributes.Add("value", tempPwd)


                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
            End If



        Catch ex As System.Data.UpdateException
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.InnerException.Message + "')", True)
            chkPassSave = 0
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกผิดพลาด')", True)
            chkPassSave = 0

        End Try

        If (chkPassSave = 1) Then
            saveLogEdit()
            Response.Redirect("systemConfig.aspx")
        End If

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("systemConfig.aspx")
    End Sub
#End Region


#Region " Function "


    Private Function chkBeforeSave() As Boolean
        Dim result As Boolean = True
        Dim intTestInteger As Integer = 0

        If (txtExpirePwdAdmin.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 1 " & "กรุณากรอกจำนวนวัน ที่รหัสผ่าน Admin หมดอายุ" & "\n"

        Else
            Try
                intTestInteger = txtExpirePwdAdmin.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 1 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
        End If



        If (txtExpirePwdUser.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 2 " & "กรุณากรอกจำนวนวัน ที่รหัสผ่าน User หมดอายุ" & "\n"
        Else
            Try
                intTestInteger = txtExpirePwdUser.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 2 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try

        End If




        If (txtNumFalseLogin.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 3 " & "กรุณากรอกจำนวนครั้งที่ login ผิดพลาด" & "\n"
        Else
            Try
                intTestInteger = txtNumFalseLogin.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 3 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
           
        End If

        If (txtGetLogFile.Text = "") Then
            result = False

            strMsg = strMsg & " ช่องที่ 4 " & "กรุณากรอกการกำหนดให้มีการเก็บ log file กี่วัน" & "\n"
        Else
            Try
                intTestInteger = txtGetLogFile.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 4 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
           
        End If



        If (txtAlertBeforeExpirePwd.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 5 " & "กรุณากรอกการการแจ้งเตือนให้ผู้ใช้ให้เปลี่ยน password " & "\n"
        Else
            Try
                intTestInteger = txtAlertBeforeExpirePwd.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 5 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
        End If




        If (txtAlertBeforeExpireUser.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 6 " & "กรุณากรอกการการแจ้งเตือนวันหมดอายุของผู้ใช้งาน" & "\n"
        Else
            Try
                intTestInteger = txtAlertBeforeExpireUser.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 6 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
        End If

        If (txtDefaultExpirePwdUser.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 7 " & "กรุณากรอกการกำหนดค่าตั้งต้นวันหมดอายุของผู้ใช้งาน" & "\n"
        Else
            Try
                intTestInteger = txtDefaultExpirePwdUser.Text
            Catch ex As Exception
                strMsg = strMsg & " ช่องที่ 7 " & "กรุณากรอกจำนวนวันเป็นตัวเลข" & "\n"
                result = False
            End Try
        End If


        Dim clsCheckPwd As New clsCheckPwd


        If (txtDefaultPassword.Text = "") Then
            result = False
            strMsg = strMsg & " ช่องที่ 8 " & "กรุณากรอกการกำหนดค่าตั้งต้นรหัสผ่าน" & "\n"
        Else
            If Not clsCheckPwd.checkpassword(txtDefaultPassword.Text) Then
                strMsg = strMsg & " ช่องที่ 8 " & clsCheckPwd.Msg & "\n"
                result = False
            End If
        End If
        clsCheckPwd = Nothing
        Return result

    End Function

    Sub saveLogEdit()
        Dim desc As String = "บันทึก แก้ไข ตั้งค่าระบบ"

        Dim clsLogTransaction As New clsLogTransaction
        clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 4, desc, "M61")
        clsLogTransaction = Nothing
    End Sub

#End Region

    Dim clsDataTable As clsLINQToDataTable
    Dim purgeJobEntity As PurgeEntities

    Private Sub createPurgeSQL(ByVal pFilePath As String, ByVal pFileName As String, ByVal pMonth As String)
        Dim fp As StreamWriter
        Dim dsPurgeDetail As DataSet
        clsDataTable = New clsLINQToDataTable

        purgeJobEntity = New PurgeEntities
        Dim PurgeQry = From purgeJob In purgeJobEntity.SPURGEJOB
                     Select purgeJob.PURGE_TABLE, purgeJob.PURGE_DATE_NAME, purgeJob.PURGE_DESC, purgeJob.PURGE_TO


        dsPurgeDetail = New DataSet()
        dsPurgeDetail.Tables.Add(clsDataTable.LINQToDataTable(PurgeQry, "PurgeDetail").Copy)
        fp = File.CreateText(pFilePath & pFileName)

        fp.WriteLine(" SET DEFINE OFF;")
        For Each dr As DataRow In dsPurgeDetail.Tables("PurgeDetail").Rows
            fp.WriteLine("insert into " & dr("PURGE_TO").ToString & "  value  (select * from " & dr("PURGE_TABLE").ToString & " where  MONTHS_BETWEEN( TRUNC(SYSDATE, 'MM')  , TRUNC(  " & dr("PURGE_DATE_NAME").ToString & " , 'MM')  )  > " & pMonth & " ); ")
            fp.WriteLine("delete from " & dr("PURGE_TABLE").ToString & "  where  MONTHS_BETWEEN( TRUNC(SYSDATE, 'MM')  , TRUNC(" & dr("PURGE_DATE_NAME").ToString & " , 'MM')  )  > " & pMonth & "; ")
            fp.WriteLine("update  SPURGEJOB set  LAST_PURGE_DATE =  sysdate where PURGE_TABLE =  '" & dr("PURGE_TABLE").ToString & "' ; ")
            fp.WriteLine("commit ;")
        Next

        fp.Close()
    End Sub
End Class
